package znbkTestPaperBj.fragment;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.utils.DialogHelper;
import com.example.lancooznbk.view.base.BaseFragment;

import java.util.ArrayList;

import lancooControl.AnswerViewPagerAdapter;
import lancooCustomListview.BounceListView;
import znbkTopicType.WrapBaseAdapter;
import tcking.github.com.giraffeplayer.GiraffePlayer;
import znbkTopicType.resContent.ZnbkContentResSplAdapter;
import znbkTopicType.resContent.ZnbkContentResStlDxAdapter;
import znbkTopicType.resContent.ZnbkContentResStlPptAdapter;
import znbkTopicType.resContent.ZnbkContentResStlPtAdapter;
import znbkTopicType.resContent.ZnbkContentResStlTlAdapter;
import znbkTopicType.resContent.ZnbkContentResStlZwAdapter;
import znbkTopicType.resContent.ZnbkContentResTplAdapter;
import znbkTopicType.resContent.ZnbkContentResWblAdapter;
import znbkTopicType.resContent.ZnbkContentResYplAdapter;
import znbkTopicType.resResponse.ZnbkResponseResStlJdtAdapter;
import znbkTopicType.resResponse.ZnbkResponseResStlPptAdapter;
import znbkTopicType.resResponse.ZnbkResponseResStlTktAdapter;
import znbkTopicType.resResponse.ZnbkResponseResStlXztAdapter;

import static znbkTestPaperBj.activity.NoteCheckOriginalQuestionPaperActivity.PAPER_FROM;
import static znbkTestPaperBj.activity.NotePaperActivity.mEntityBj;

/**
 * Created by Administrator on 2018\3\23 0023.
 */

public class NoteCheckOriginalQuestionPaperFragment extends BaseFragment {

    private int mNum; //页号
    private LinearLayout mLayoutTitleType;
    private TextView mTvTitleType;
    private ArrayList<View> list;
    private BounceListView LvContent;//试题主干部分
    public ViewPager VpAnswer;
    private WrapBaseAdapter mAdapter;
    private LinearLayout LayoutSelfRate;
    private int rateID;
    private int layoutId;
    private int mModelFrom;
    private FrameLayout mFlLoading;
    private RelativeLayout relContainer;

    private TextView mTvTypeName;//题目类型
    private LinearLayout mLayoutBasicInfor;//基本信息：创建时间，推荐人气
    private LinearLayout mLayoutTools;//工具栏：笔记，知识点


    public static NoteCheckOriginalQuestionPaperFragment newInstance(int num) {
        NoteCheckOriginalQuestionPaperFragment fragment = new NoteCheckOriginalQuestionPaperFragment();
        Bundle args = new Bundle();
        args.putInt("num", num);
        fragment.setArguments(args);
        return fragment;
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //这里我只是简单的用num区别标签，其实具体应用中可以使用真实的fragment对象来作为叶片
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        mModelFrom = getHolderActivity().getIntent().getIntExtra(PAPER_FROM,0);
        rateID = 0;
    }

    @Override
    protected void updateViews(boolean isShowLoading) {

        if(getResType().equals("1")||
                getResType().equals("2")||
                getResType().equals("3")||
                getResType().equals("4")||
                getResType().equals("5")||
                getResType().equals("6")){
            VpAnswer.setVisibility(View.GONE);
            showCommonTesePaper(false);
        }else{
            if(getTypeName().equals("单选题")||
                    getTypeName().equals("作文")||
                    getTypeName().equals("翻译题")){
                VpAnswer.setVisibility(View.GONE);
                showCommonTesePaper(false);
            }else{
                VpAnswer.setVisibility(View.VISIBLE);
                showCommonTesePaper(true);
            }

        }

        relContainer.post(new Runnable() {
            @Override
            public void run() {
                mFlLoading.setVisibility(View.GONE);
                if (layoutId == R.layout.paper_zgt||layoutId == R.layout.paper_dxt) {
                    DialogHelper.showGuideOne(getHolderActivity());
                    // DialogHelper.showPopup(GuessTestingPaperFragment.this);
                }else{
                    DialogHelper.showGuideTwo(getHolderActivity());
                }
            }
        });
    }

    @Override
    public int attachLayoutRes() {

        return R.layout.znbk_paper_res;
    }


    @Override
    public void initViews(View view, Bundle savedInstanceState) {

        mLayoutTitleType = (LinearLayout) view.findViewById(R.id.layout_titleType);
        mLayoutTitleType.setVisibility(View.VISIBLE);
        mTvTitleType = (TextView) view.findViewById(R.id.tv_titleType);
        mTvTitleType.setText("原题");
        mFlLoading = (FrameLayout) view.findViewById(R.id.fl_ques_loading);
        relContainer = (RelativeLayout) view.findViewById(R.id.common_main);
        LayoutSelfRate = (LinearLayout) view.findViewById(R.id.layout_self_rate);
        LvContent = (BounceListView) view.findViewById(R.id.listview_content);
        VpAnswer = (ViewPager) view.findViewById(R.id.viewpage_answer);

        mTvTypeName = (TextView) view.findViewById(R.id.tv_typeName);
        /*
        * 显示题型信息
        * */
        String GenreName = mEntityBj.getModelTKQue().getGenreName();
        String TypeName = mEntityBj.getModelTKQue().getTypeName();
        String ResTypeName =  mEntityBj.getModelTKQue().getResTypeName();
        if(TypeName.equals("")){
            if(!GenreName.equals("")) {
                TypeName = GenreName;
            }else{
                TypeName = ResTypeName;
            }
        }
        mTvTypeName.setText(TypeName);
        mLayoutBasicInfor = (LinearLayout) view.findViewById(R.id.layout_basicInfor);
        mLayoutBasicInfor.setVisibility(View.GONE);
        mLayoutTools = (LinearLayout) view.findViewById(R.id.layout_tools);
        mLayoutTools.setVisibility(View.GONE);

    }


    public static GiraffePlayer player;
    /*
    * @@显示试卷主体
    * 实现试卷界面的布局展示
    * */
    private void showCommonTesePaper(boolean bShowChildViewPager){

         /*
        * 试题主干显示区
        * */

        if(getResType().equals("1")){
            mAdapter = new ZnbkContentResWblAdapter(getActivity(), 1, mNum, mModelFrom);
        }else if(getResType().equals("2")){
            mAdapter = new ZnbkContentResTplAdapter(getActivity(), 1, mNum, mModelFrom);
        }else if(getResType().equals("3")||
                getResType().equals("5")){
            mAdapter = new ZnbkContentResYplAdapter(getActivity(), 1, mNum, mModelFrom);
        }else if(getResType().equals("4")||
                getResType().equals("6")){
            mAdapter = new ZnbkContentResSplAdapter(getActivity(), 1, mNum, mModelFrom);
            player = new GiraffePlayer(getActivity());
            player.play(mEntityBj.getModelTKQue().getVedioList().get(0).getAudioPath());
            player.setShowNavIcon(false);
        }else{

            if(getTypeName().equals("作文")||
                    getTypeName().equals("翻译题")){
                mAdapter = new ZnbkContentResStlZwAdapter(getActivity(), 1, mNum, mModelFrom);

            }else if(getTypeName().equals("单选题")){
                mAdapter = new ZnbkContentResStlDxAdapter(getActivity(), 1, mNum, mModelFrom);
            }
            //听力选择
            else if(getTypeName().equals("听力选择")){
                mAdapter = new ZnbkContentResStlTlAdapter(getActivity(), 1, mNum, mModelFrom);
            }
            //匹配题
            else if(getTypeName().equals("匹配题")){
                mAdapter = new ZnbkContentResStlPptAdapter(getActivity(), 1, mNum, mModelFrom);
            }
            //完型选择，阅读理解,阅读填空,改错题
            else {
                mAdapter = new ZnbkContentResStlPtAdapter(getActivity(), 1, mNum, mModelFrom);
            }

        }

        LvContent.setAdapter(mAdapter);

        if(bShowChildViewPager) {

            /*
            * 答题区
            * */
            list = new ArrayList<View>();
            list.clear();
            int sumSubject = getChildItemCount(mNum);
            for (int k = 0; k < sumSubject; k++) {

                BounceListView LvAnswer = new BounceListView(getActivity());

                if(getTypeName().equals("阅读简答")){
                    LvAnswer.setAdapter(new ZnbkResponseResStlJdtAdapter(getActivity(),1,mNum,k,mModelFrom));
                }
                //匹配题
                else if(getTypeName().equals("匹配题")){
                    LvAnswer.setAdapter(new ZnbkResponseResStlPptAdapter(getActivity(),1,mNum,k,mModelFrom));
                }
                //阅读填空，词汇填空，改错题
                else if(getTypeName().equals("阅读填空")||
                        getTypeName().equals("词汇填空")||
                        getTypeName().equals("改错题")){
                    LvAnswer.setAdapter(new ZnbkResponseResStlTktAdapter(getActivity(),1,mNum,k,mModelFrom));
                }
                //完型选择，阅读理解
                else{
                    LvAnswer.setAdapter(new ZnbkResponseResStlXztAdapter(getActivity(),1,mNum,k,mModelFrom));
                }

                list.add(LvAnswer);
            }

            VpAnswer.setAdapter(new AnswerViewPagerAdapter(list));
            VpAnswer.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    //此处添加父页面操作事件
                    rateID = position;
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }


    }


    /*
    * 获取试卷总页数
    * */
    private int getQusSum(){
        return 1;
    }

    /*
    * 获取题目对应的小题数目(显示页数)
    * */
    private int getChildItemCount(int position){

        int sumChildItem = -1;
        int childItemCount = mEntityBj.getModelTKQue().getChildList().size();
        int totalItemCount = mEntityBj.getModelTKQue().getTotalItemCount();
        if(totalItemCount==childItemCount||mEntityBj.getModelTKQue().getTypeName().equals("匹配题")){
            sumChildItem = totalItemCount;
        }else{
            sumChildItem = 1;
        }
        return sumChildItem;

    }

    private String getTypeName() {
        return mEntityBj.getModelTKQue().getTypeName();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!isVisibleToUser && mAdapter != null){//可见时进行调用
            mAdapter.pause();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAdapter != null){//不可见 暂停音乐播放
            mAdapter.pause();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mAdapter != null){//不可见 暂停音乐播放
            mAdapter.pause();
        }

        if (player != null) {
            player.onPause();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mAdapter != null) {
            mAdapter.stop();
        }

        if (player != null) {
            player.onDestroy();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (player != null) {
            player.onResume();
        }
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (player != null) {
            player.onConfigurationChanged(newConfig);
        }
    }

    private String getResTypeName(){
        return mEntityBj.getModelTKQue().getResTypeName();
    }

    private String getResType(){
        return mEntityBj.getModelTKQue().getResType();
    }


}